%% Real cola bottle
close all;

cola_liquid_volume=0.1:5:185.1;

cola_bottle_x=[0 1 1.8 1.8 2 2.2 2.2 3:29 28.5 28.2 27.8 27.5];
cola_bottle_y=[1.3 1.3 1.3 1.8 1.8 1.8 1.5 1.6 2.1 2.5 2.9 3.2 3.6 3.9 4 4 4 4 4 4 4 4 4 4 3.9 3.8 3.7 3.6 3.8 3.9 4 4 3.6 3 2.3 1.8 1.5 0];
cola_bottle_cap_x=[1.4 0 0 0 1.4];
cola_bottle_cap_y=[-1.5 -1.5 0 1.5 1.5];
cola_liquid_density = 1000/185; %over max_volume
cola_bottle_mass = 73; %Grams
cola_cap_mass = 1; %Grams

[cola_bottle_mass_center, cola_bottle_shape, cola_cap_mass_center] = bottle_model(cola_bottle_x, cola_bottle_y, cola_bottle_cap_x, cola_bottle_cap_y);

cola_mode_parameters.mode_name = 'mesh';
cola_edge_length = 3;
figure(3);
[cola_mode_parameters.vertices cola_mode_parameters.triangles] = triangulation(cola_bottle_shape, cola_edge_length);
cola_mode_parameters.rotation_angle = 0;

cola_angle = zeros(1, length(cola_liquid_volume));
for i=1:length(cola_liquid_volume)
    tic;
    cola_angle(i) = tipping_angle(cola_liquid_volume(i), cola_liquid_density, cola_bottle_shape, cola_bottle_mass,...
        cola_bottle_mass_center, cola_cap_mass, cola_cap_mass_center, cola_mode_parameters);
    toc
end;

cola_real_data=[0 41.7 50 100;12 22.7 21 13];

cola_liquid_volume=cola_liquid_volume/1.851;
cola_angle = -cola_angle*180/pi;

p=polyfit(cola_liquid_volume,cola_angle,4);
f = polyval(p,cola_liquid_volume);
max_percentage=cola_liquid_volume(f==max(f));
figure(4)
subplot(3,2,1);
plot(cola_liquid_volume, cola_angle,'bx', cola_liquid_volume,f,'r-',...
    cola_real_data(1,:),cola_real_data(2,:),'ko', 'linewidth',2);
hold on;
hleg = legend('Mesh Method','Fitting','Experimental Data');
set(hleg, 'Position', [0.45,.1,.2,.3]);
hold off;
title('Coke bottle');

%% Mineral water
mineral_water_liquid_volume = [0.1:5:133.1 133];

mineral_water_bottle_x=[0:21 21];
mineral_water_bottle_y=1+[0 0 0.5 2.8 2.8 2 1.5 1.4 2 2.8 2.8 2.8 2.8 2.8 2.8 2.8 2.8 2.8 2.8 2.8 2.8 2.5 1];
mineral_water_bottle_cap_x=[0 -1 -1 -1 0]+1;
mineral_water_bottle_cap_y=[-1 -1 0 1 1];
mineral_water_liquid_density = 500/133; %over max_volume
mineral_water_bottle_mass = 15; %Grams
mineral_water_cap_mass = 1; %Grams

[mineral_water_bottle_mass_center, mineral_water_bottle_shape, mineral_water_cap_mass_center] = bottle_model(mineral_water_bottle_x, mineral_water_bottle_y, mineral_water_bottle_cap_x, mineral_water_bottle_cap_y);

mineral_water_mode_parameters.mode_name = 'mesh';
mineral_water_edge_length = 3;
figure(3);
[mineral_water_mode_parameters.vertices mineral_water_mode_parameters.triangles] = triangulation(mineral_water_bottle_shape, mineral_water_edge_length);
mineral_water_mode_parameters.rotation_angle = 0;

mineral_water_angle = zeros(1, length(mineral_water_liquid_volume));
for i=1:length(mineral_water_liquid_volume)
    tic;
    mineral_water_angle(i) = tipping_angle(mineral_water_liquid_volume(i), mineral_water_liquid_density, mineral_water_bottle_shape, mineral_water_bottle_mass,...
        mineral_water_bottle_mass_center, mineral_water_cap_mass, mineral_water_cap_mass_center, mineral_water_mode_parameters);
    toc
end;

mineral_water_real_data=[0 25 50 100;8 27 22 12];

mineral_water_liquid_volume=mineral_water_liquid_volume/1.33;
mineral_water_angle = -mineral_water_angle * 180 / pi;

p2=polyfit(mineral_water_liquid_volume,mineral_water_angle,4);
f2 = polyval(p2,mineral_water_liquid_volume);
max_percentage2=mineral_water_liquid_volume(f2==max(f2));
figure(4)
subplot(3,2,3);
plot(mineral_water_liquid_volume, mineral_water_angle,'bx', mineral_water_liquid_volume,f2,'r-', 'linewidth',2);
title('Water bottle');

ylabel({'Maximal          ';'tipping          ';'angle         ';...
    '(deg)          '},'rot',0);
%% Cylinder
cylinder_liquid_volume=0.1:4:80;

cylinder_bottle_x=0:20;
cylinder_bottle_y=2*ones(1,length(cylinder_bottle_x));
cylinder_bottle_cap_x=[0 -1 -1 -1 0]+1;
cylinder_bottle_cap_y=[-1 -1 0 1 1];
cylinder_liquid_density = 250/80; %over max_volume
cylinder_bottle_mass = 50; %Grams
cylinder_cap_mass = 0; %Grams

[cylinder_bottle_mass_center, cylinder_bottle_shape, cylinder_cap_mass_center] = bottle_model(cylinder_bottle_x, cylinder_bottle_y, cylinder_bottle_cap_x, cylinder_bottle_cap_y);

cylinder_mode_parameters.mode_name = 'mesh';
cylinder_edge_length = 3;
figure(3);
[cylinder_mode_parameters.vertices cylinder_mode_parameters.triangles] = triangulation(cylinder_bottle_shape, cylinder_edge_length);
cylinder_mode_parameters.rotation_angle = 0;

cylinder_angle = zeros(1, length(cylinder_liquid_volume));
for i=1:length(cylinder_liquid_volume)
    tic;
    cylinder_angle(i) = tipping_angle(cylinder_liquid_volume(i), cylinder_liquid_density, cylinder_bottle_shape, cylinder_bottle_mass,...
        cylinder_bottle_mass_center, cylinder_cap_mass, cylinder_cap_mass_center, cylinder_mode_parameters);
    toc
end;


cylinder_liquid_volume=cylinder_liquid_volume/0.8;
cylinder_angle = -cylinder_angle * 180 / pi;

p3=polyfit(cylinder_liquid_volume,cylinder_angle,4);
f3 = polyval(p3,cylinder_liquid_volume);
max_percentage3=cylinder_liquid_volume(f3==max(f3));
figure(4)
subplot(3,2,5);
plot(cylinder_liquid_volume, cylinder_angle,'bx',cylinder_liquid_volume,f3,'r-','linewidth',2);
title('Cylinder');

xlabel('Volume (%)');

%% Wine bottle
wine_liquid_volume=0.1:10:140.1;

wine_bottle_x=[0 5 7 9 11 17.5];
wine_bottle_y=[1 1 1.5 5.5 7 7.5];
wine_bottle_cap_x=[0 -1 -1 -1 0]+1;
wine_bottle_cap_y=[-1 -1 0 1 1];
wine_liquid_density = 500/140.1; %over max_volume
wine_bottle_mass = 300; %Grams
wine_cap_mass = 0; %Grams

[wine_bottle_mass_center, wine_bottle_shape, wine_cap_mass_center] = bottle_model(wine_bottle_x, wine_bottle_y, wine_bottle_cap_x, wine_bottle_cap_y);

wine_mode_parameters.mode_name = 'mesh';
wine_edge_length = 3;
figure(3);
[wine_mode_parameters.vertices wine_mode_parameters.triangles] = triangulation(wine_bottle_shape, wine_edge_length);
wine_mode_parameters.rotation_angle = 0;

wine_bottle_angle = zeros(1, length(wine_liquid_volume));
for i=1:length(wine_liquid_volume)
    tic;
    wine_bottle_angle(i) = tipping_angle(wine_liquid_volume(i), wine_liquid_density, wine_bottle_shape, wine_bottle_mass,...
        wine_bottle_mass_center, wine_cap_mass, wine_cap_mass_center, wine_mode_parameters);
    toc
end;


wine_liquid_volume = wine_liquid_volume / 1.401;
wine_bottle_angle = -wine_bottle_angle * 180 / pi;

p4=polyfit(wine_liquid_volume,wine_bottle_angle,4);
f4 = polyval(p4,wine_liquid_volume);
max_percentage4=wine_liquid_volume(f4==max(f4));
figure(5);
plot(wine_liquid_volume, wine_bottle_angle,'bx',wine_liquid_volume,f4,'r-','linewidth',2);
title('Wine bottle');